package com.ccepc.algmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ccepc.algmanage.utils.CommonUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.common.log.annotation.SysLog;
import com.ccepc.algmanage.service.LinkedDeviceService;
import com.ccepc.demo.api.entity.LinkedDevice;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 *
 *
 * @author pigx code generator
 * @date 2023-11-02 14:45:05
 */
@RestController
@RequestMapping("/linkeddevice" )
@Api(value = "linkeddevice", tags = "联动设备")
public class LinkedDeviceController {

    @Resource
    private LinkedDeviceService linkedDeviceService;

    @ApiOperation(value = "通过deviceNo查询")
    @GetMapping("/{deviceNo}" )
    @PreAuthorize("@pms.hasPermission('algmanage_linkeddevice_view')" )
    public R getById(@PathVariable("deviceNo" ) String deviceNo) {
        return R.ok(linkedDeviceService.getOne(
                new LambdaQueryWrapper<LinkedDevice>()
                        .eq(LinkedDevice::getDeviceNo, deviceNo))
        );
    }

    @ApiOperation(value = "联动设备新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('algmanage_linkeddevice_add')" )
    public R save(@RequestBody LinkedDevice linkedDevice) {
        if (linkedDevice.getLinkedIds().isEmpty()) {
            return R.ok(linkedDeviceService.remove(
                    new LambdaQueryWrapper<LinkedDevice>()
                            .eq(LinkedDevice::getDeviceNo, linkedDevice.getDeviceNo())
            ));
        }
        return R.ok(CommonUtils.saveOrUpdate(linkedDevice, linkedDeviceService));
    }

    @ApiOperation(value = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('algmanage_linkeddevice_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(linkedDeviceService.removeById(id));
    }

}
